package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;

    /**
     * 根据条件分页查询租约列表
     *
     * @param page    分页信息
     * @param queryVo 查询条件
     * @return 当前页信息
     */
    @Override
    public IPage<AgreementVo> getLeaseAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.selectLeaseAgreementByQuery(page, queryVo);
    }

    /**
     * 根据id查询租约信息
     *
     * @param id id
     * @return 租约信息
     */
    @Override
    public AgreementVo getAgreementById(Long id) {
        // 查询租约基础信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        // 查询签约公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(leaseAgreement.getApartmentId());
        // 查询签约房间的信息
        RoomInfo roomInfo = roomInfoService.getById(leaseAgreement.getRoomId());
        // 查询支付方式信息
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        // 查询租期信息
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());

        // 新建返回租约对象Vo
        AgreementVo agreementVo = new AgreementVo();
        // 封装基础信息
        BeanUtils.copyProperties(leaseAgreement, agreementVo);
        // 封装签约公寓信息
        agreementVo.setApartmentInfo(apartmentInfo);
        // 封装签约房间的信息
        agreementVo.setRoomInfo(roomInfo);
        // 封装支付方式信息
        agreementVo.setPaymentType(paymentType);
        // 封装租期信息
        agreementVo.setLeaseTerm(leaseTerm);

        return agreementVo;
    }
}




